package utilclass;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

import org.junit.jupiter.api.Test;

public class UtilClassDemo {
	@Test
	public void random() {
		//1.随机数对象
		Random rand = new Random();
		System.out.println("产生一个整数随机:" + rand.nextInt());
		System.out.println("产生一个整数随机带范围:" + rand.nextInt(100));
		System.out.println("产生一个双精浮点随机:" + rand.nextDouble());
		System.out.println("产生一个单精度随机:" + rand.nextFloat());
		System.out.println("产生一个布尔随机:" + rand.nextBoolean());
	}
	
	@Test
	public void scanner() {
		Scanner scan = new Scanner(System.in);
		//输入一行字符串
		String nextLine = scan.nextLine();
		System.out.println(nextLine);
		//输入一个整数
		int i = scan.nextInt();
		System.out.println(i);
		//输入一个布尔
		boolean b = scan.nextBoolean();
		System.out.println(b);
		// 输入一个double
		double d = scan.nextDouble();
		System.out.println(d);
		//输入字符串
		String next = scan.next();
		System.out.println(next);
	}
	
	@Test
	public void math() {
		//Math类全是静态常量和静态方法
		System.out.println(Math.PI);
		System.out.println(Math.E);
		System.out.println("abs绝对值:" + Math.abs(-5));
		System.out.println("max求最大值:" + Math.max(100, 50));
		System.out.println("min求最小值:" + Math.min(100,50));
		System.out.println("sqrt求平方根:" + Math.sqrt(16));
		System.out.println("pow求幂值:" + Math.pow(2, 3));
		System.out.println("floor向下取整:" + Math.floor(3.4));
		System.out.println("ceil向上取整:" + Math.ceil(3.4));
		System.out.println("round四舍五入:" + Math.round(4.54));
	}
	
	@Test
	public void date() {
		//1.创建一个日期类
		Date d = new Date();  //获取系统的当前时间
		System.out.println(d);
		//2.简单日期格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String f = sdf.format(d);
		System.out.println(f);
		
		//jdk1.8 本地日期
		LocalDate d2 = LocalDate.now(); //获取系统的当前日期
		System.out.println(d2);
		d2 = LocalDate.of(5000, 11, 11);  //给定指定日期 
		System.out.println(d2);
		//jdk1.8 本地时间
		LocalTime t = LocalTime.now();   //获取系统的当前时间
		System.out.println(t);
		t = LocalTime.of(13, 23,45);  //给定指定时间
		System.out.println(t);
		//jdk1.8 本地日期时间
		LocalDateTime dt = LocalDateTime.now();
		System.out.println(dt);
		dt = LocalDateTime.of(2022, 10, 5, 11, 11,11);
		System.out.println(dt);
		
		//日历（单例）
		Calendar c = Calendar.getInstance();
		System.out.println(c.get(Calendar.YEAR));
		System.out.println(c.get(Calendar.MONTH)+1);
		System.out.println(c.get(Calendar.DAY_OF_MONTH));
		c.add(Calendar.YEAR, 10);
		System.out.println(c);
	}
	
	@Test
	public void packageClass() {
		//每个基本类型都有自己的包装类（封装类）
		Integer i = 10;  //  Integer i = new Integer(10);  自动装箱
		int j = i;      //  int j = i.intValue();   自动拆箱
		Integer num = Integer.valueOf("123");
		System.out.println(num);
		
		Double num2 = Double.valueOf("1.234");
		System.out.println(num2);
		
		// char->Character
		System.out.println(Character.isLetter('a'));
		System.out.println(Character.isDigit('0'));
		System.out.println(Character.isUpperCase('A'));
		System.out.println(Character.isLowerCase('A'));
		System.out.println(Character.toLowerCase('A'));
		System.out.println(Character.toUpperCase('w'));
	}
	
	@Test
	public void system() {
		//System系统类
		//1.得到系统时间,从计算机元年到现在经过的毫秒数
		System.out.println(System.currentTimeMillis());
		//2.调用垃圾收集,把堆中没有被引用的对象空间释放掉 
		System.gc();   
		//3.数组拷贝
		char[] arr = "hello kitty".toCharArray();
		char[] copy = new char[arr.length];
		System.arraycopy(arr, 0, copy, 0, arr.length);
		System.out.println(copy);
		//4.退出程序,0是正常退出程序，非0是非正常退出
		System.exit(0);
	}
	
	@Test
	public void objects() {
		//Objects是Object的工具类
		String str = null;
		String str2 = "abc";
		//1.比较两个对象是否相等
		System.out.println(Objects.equals(str, str2));
		//2.判断是否空
		System.out.println(Objects.isNull(str2));
		//3.比较任意两个对象并且给定比较器
		int compare = Objects.compare(
				Integer.valueOf(100), 
				Integer.valueOf(200), 
				new Comparator<Integer>() {
					@Override
					public int compare(Integer o1, Integer o2) {
						return o1-o2;
					}
				}
		);
		System.out.println(compare);
	}
	
	@Test
	public void arrays() {
		//Arrays -> 数组的工具类
		//1.把数组变成字符串
		int[] arr = {1,2,41,1,41,1,352};
		System.out.println(Arrays.toString(arr));
		//2.数组填充
		int[] arr2 = new int[10];
		Arrays.fill(arr2, 100);
		System.out.println(Arrays.toString(arr2));
		//3.排序,从小到大
		Arrays.sort(arr);
		System.out.println(Arrays.toString(arr));
		//4.二分法查找：从头和尾一起往中间找下标，位置从0开始
		int location = Arrays.binarySearch(arr, 1);
		System.out.println(location);
		//5.数组拷贝，第一个参数是数组名，第二个参数是新数组的空间
		int[] copy = Arrays.copyOf(arr, 10);
		System.out.println(Arrays.toString(copy));
		//6.使用此方法创建的list的!!!长度不能变(不能添加和删除元素)
		List<String> asList = Arrays.asList("1","2","3","4","5","6","76");
		System.out.println(asList);
	}
	
	@Test
	public void collections() {
		//Collections是集合的工具类
		List<Integer> list = new ArrayList<>();
		//1.添加元素
		Collections.addAll(list, 1,2,3,4,56,68,466,489,65,31,4,5,6,5);
		System.out.println(list);
		//2.排序
		Collections.sort(list);
		System.out.println(list);
		//3.必须在已经排好序的集合才能查找下标,位置从0开始
		int index = Collections.binarySearch(list, 5);
		System.out.println(index);
		//4.逆序
		Collections.reverse(list);
		System.out.println(list);
		//5.乱序
		Collections.shuffle(list);
		System.out.println(list);
		//6.填充
		Collections.fill(list, 99);
		System.out.println(list);
		//7.得到一堆空集合
		List<Object> emptyList = Collections.emptyList();
		System.out.println(emptyList);
		Set<Object> emptySet = Collections.emptySet();
		System.out.println(emptySet);
		Map<Object, Object> emptyMap = Collections.emptyMap();
		System.out.println(emptyMap);
	}
	
	@Test
	public void files() throws IOException {
		//Files是File的工具类
		Path p1 = Paths.get("a.txt");  //得到路径
		Path p2 = Paths.get("a_copy.txt");
		//1.文件拷贝
		//Files.copy(p1, p2);
		//2.创建一个文件
		Path p3 = Paths.get("new.txt");
		//Files.createFile(p3);
		//3.删除一个文件
		Files.delete(p3);
	}
	
	@Test
	public void bigNumber() {
		//1.大整数
		BigInteger bi = BigInteger.valueOf(123142131312141L);
		BigInteger bi2 = new BigInteger("1231321313213132131321313123132132131");
		BigInteger bi3 = new BigInteger("1231321313213132131321313123132132131");
		// +-*/%
		System.out.println(bi2.add(bi3));
		System.out.println(bi2.subtract(bi3));
		System.out.println(bi2.multiply(bi3));
		System.out.println(bi2.divide(bi3));
		System.out.println(bi2.mod(bi3));
		//2.大浮点
		BigDecimal pi = new BigDecimal("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679");
		BigDecimal pi2 = new BigDecimal("3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679");
		System.out.println(pi.add(pi2));
		System.out.println(pi.subtract(pi2));
		System.out.println(pi.multiply(pi2));
		System.out.println(pi.divide(pi2));
	}
}











